package com.msb.mall.member.controller;

import java.util.Arrays;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.msb.common.exception.BizCodeEnume;
import com.msb.mall.member.exception.PhoneExistException;
import com.msb.mall.member.exception.UsernameExistException;
import com.msb.mall.member.vo.MemberLoginVO;
import com.msb.mall.member.vo.MemberReigerVO;
import com.msb.mall.member.vo.SocialUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.msb.mall.member.entity.MemberEntity;
import com.msb.mall.member.service.MemberService;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;

import javax.swing.plaf.basic.BasicIconFactory;


/**
 * 会员
 *
 * @author zhangjiajun
 * @email 1606516025@qq.com
 * @date 2025-05-08 21:54:50
 */
@RestController
@RequestMapping("/member/member")
public class MemberController {

    @Autowired
    private MemberService memberService;


    /**
     * 登录
     *
     * @param vo
     * @return
     */
    @RequestMapping("/oauth/login")
    public R socialLogin(@RequestBody SocialUser vo) {
        MemberEntity entity = memberService.socialLogin(vo);


        return R.ok().put("entity", JSON.toJSONString(entity));
    }


    /**
     * 登录
     *
     * @param vo
     * @return
     */
    @RequestMapping("/login")
    public R login(@RequestBody MemberLoginVO vo) {
        MemberEntity entity = memberService.login(vo);
        if (entity != null) {
            return R.ok().put("entity", JSON.toJSONString(entity));
        }

        return R.error(BizCodeEnume.USERNAME_PHONE_VALID_EXCEPTION.getCode(),
                BizCodeEnume.USERNAME_PHONE_VALID_EXCEPTION.getMsg());
    }


    /**
     * 会员注册
     *
     * @param vo
     * @return
     */
    @PostMapping("/register")
    public R register(@RequestBody MemberReigerVO vo) {
        try {
            memberService.register(vo);
        } catch (UsernameExistException e) {
            return R.error(BizCodeEnume.USERNAME_EXIST_EXCEPTION.getCode(), BizCodeEnume.USERNAME_EXIST_EXCEPTION.getMsg());
        } catch (PhoneExistException e) {
            return R.error(BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode(), BizCodeEnume.PHONE_EXIST_EXCEPTION.getMsg());
        } catch (Exception e) {
            return R.error(BizCodeEnume.UNKNOW_EXCEPTION.getCode(), BizCodeEnume.UNKNOW_EXCEPTION.getMsg());
        }
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member) {
        memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member) {
        memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
